package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVo;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.objects.annotations.Setter;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.Oneway;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 前端目的
 */
@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyEsService strategyEsService;

    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> top5 = strategyCommendService.getTop5();
        model.addAttribute("commends",top5);

       // hotCds
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.queryHotSortTop10();
        model.addAttribute("hotCds",hotCds);

        //abroadCds
       List<StrategyStatisVO> abroadCds =  strategyStatisRedisService.queryAbroadSortTop10();
       model.addAttribute("abroadCds",abroadCds);
        //unabroadCds
        List<StrategyStatisVO> unabroadCds =  strategyStatisRedisService.queryChinaSortTop10();
        model.addAttribute("unabroadCds",unabroadCds);
        //主题推荐
        //themeCds
        //优化   倒排 取前10   目的地去重
        List<Map<String ,Object>> data = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds",data);

        //chinas
        List<StatisVo> chinas=strategyEsService.aueryConditionChina();
        model.addAttribute("chinas",chinas);

        //abroads
        List<StatisVo> abroads=strategyEsService.aueryConditionAbroad();
        model.addAttribute("abroads",abroads);
        //themes
        List<StatisVo> themes=strategyEsService.aueryConditionTheme();
        model.addAttribute("themes",themes);

        return "strategy/index";
    }






    //首页
    @RequestMapping("detail")
    public String index(Model model,Long id){
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);

        //量 +1
        strategyStatisRedisService.increaseViewNum(id,1);
        //共享vo对象
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(id);
        model.addAttribute("vo",vo);

        //阅读  排行的加分
        strategyStatisRedisService.addScoreHot(id,1);


        return "strategy/detail";


    }

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);

        PageInfo page = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",page);

        return "strategy/list";

    }
//攻略评论添加
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment,@UserParam UserInfo userInfo){
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());

        strategyCommentService.save(comment);

        //评论数添加
        strategyStatisRedisService.increaseReplyNum(comment.getDetailId(),1);
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId());
        //评论  排行的加分
        strategyStatisRedisService.addScoreHot(comment.getDetailId(),1);

        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }
    //评论的列表
    @RequestMapping("/comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }

    //评论点赞
    //toid  评论的id
    //fromid  点赞  取消点赞用户的id

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumbUp(toid,userInfo.getId());
        return AjaxResult.SUCCESS;
    }
    //收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        if (userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }

        //取消收藏
        boolean ret = strategyStatisRedisService.favor(sid,userInfo.getId());
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);

        //收藏  国内排行的加分
        strategyStatisRedisService.addScoreAbroadAndChina(sid,ret?1:-1);

        return new AjaxResult(ret,"").addData(vo);
    }
     //攻略文章点赞顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        if (userInfo == null){
            //没登录
            AjaxResult result = new AjaxResult(false,"请先登录");
            result.setCode(102);
            return result;
        }
        //今天已经顶过  顶成功
        boolean ret = strategyStatisRedisService.thumbup(sid,userInfo.getId());
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);
        return new AjaxResult(ret,"").addData(vo);
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") StrategySearchQuery qo){
        Page page = strategyEsService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }


}
